
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.PUB;
using Apps.Models.PUB;
using Apps.IBLL.PUB;
using System.Data;

namespace Apps.BLL.PUB
{
	public partial class V_LOG_LMSEXCHANGEBLL: Virtual_V_LOG_LMSEXCHANGEBLL,IV_LOG_LMSEXCHANGEBLL
	{
        

	}
	public class Virtual_V_LOG_LMSEXCHANGEBLL
	{
        [Dependency]
        public IV_LOG_LMSEXCHANGERepository m_Rep { get; set; }

		public virtual List<V_LOG_LMSEXCHANGEModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<V_LOG_LMSEXCHANGE> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>  a.LOG_ID.Contains(queryStr)
								|| a.BATCH_CODE_A.Contains(queryStr)
								|| a.BATCH_CODE_B.Contains(queryStr)
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
		 
		public virtual List<V_LOG_LMSEXCHANGEModel> GetPagerList(ref GridPager pager, Expression<Func<V_LOG_LMSEXCHANGE, bool>> whereLambda )
        {

            IQueryable<V_LOG_LMSEXCHANGE> queryData = m_Rep.GetList(whereLambda); 
             
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<V_LOG_LMSEXCHANGEModel> GetList(Expression<Func<V_LOG_LMSEXCHANGE, bool>> whereLambda )
        {

            IQueryable<V_LOG_LMSEXCHANGE> queryData = m_Rep.GetList(whereLambda); 
       
            return CreateModelList(ref queryData);
        }

		public virtual List<V_LOG_LMSEXCHANGEModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<V_LOG_LMSEXCHANGEModel>();
		}

        public virtual List<V_LOG_LMSEXCHANGEModel> CreateModelList(ref IQueryable<V_LOG_LMSEXCHANGE> queryData)
        {

            List<V_LOG_LMSEXCHANGEModel> modelList = (from r in queryData
                                              select new V_LOG_LMSEXCHANGEModel
                                              {
													LOG_ID = r.LOG_ID,
													BATCH_CODE_A = r.BATCH_CODE_A,
													BATCH_CODE_B = r.BATCH_CODE_B,
													OPERATE_CONTENT = r.OPERATE_CONTENT,
													OPERATE_TIME = r.OPERATE_TIME,
													OPERATE_USER_ID = r.OPERATE_USER_ID,
													TRUENAME = r.TRUENAME,
													
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, V_LOG_LMSEXCHANGEModel model)
        {
            try
            {
                V_LOG_LMSEXCHANGE entity = m_Rep.GetById(model.LOG_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new V_LOG_LMSEXCHANGE();
               				entity.LOG_ID = model.LOG_ID;
                entity.BATCH_CODE_A = model.BATCH_CODE_A;
				entity.BATCH_CODE_B = model.BATCH_CODE_B;
				entity.OPERATE_CONTENT = model.OPERATE_CONTENT;
				entity.OPERATE_TIME = model.OPERATE_TIME;
				entity.OPERATE_USER_ID = model.OPERATE_USER_ID;
				entity.TRUENAME = model.TRUENAME;
				
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
		 public virtual bool Deletes(ref ValidationErrors errors,Expression<Func<V_LOG_LMSEXCHANGE, bool>> whereLambda)
         {
		        try
				{
					using (DBContainer db = new DBContainer())
					{
				
							foreach (var entity in db.V_LOG_LMSEXCHANGE.Where(whereLambda))
							{
								db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
							}

							if(db.SaveChanges()>0)
							{
								return true;
							}
							else
							{
								return false;
							}
			
					}
				}
				catch (Exception ex)
				{
					errors.Add(ex.Message);
					ExceptionHander.WriteException(ex);
					return false;
				}
        }

         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, V_LOG_LMSEXCHANGEModel model)
        {
            try
            {
                V_LOG_LMSEXCHANGE entity = m_Rep.GetById(model.LOG_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.LOG_ID = model.LOG_ID;
				entity.BATCH_CODE_A = model.BATCH_CODE_A;
				entity.BATCH_CODE_B = model.BATCH_CODE_B;
				entity.OPERATE_CONTENT = model.OPERATE_CONTENT;
				entity.OPERATE_TIME = model.OPERATE_TIME;
				entity.OPERATE_USER_ID = model.OPERATE_USER_ID;
				entity.TRUENAME = model.TRUENAME;
				
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual V_LOG_LMSEXCHANGEModel GetById(object id)
        {
            if (IsExists(id))
            {
                V_LOG_LMSEXCHANGE entity = m_Rep.GetById(id);
                V_LOG_LMSEXCHANGEModel model = new V_LOG_LMSEXCHANGEModel();
                              				model.LOG_ID = entity.LOG_ID;
				model.BATCH_CODE_A = entity.BATCH_CODE_A;
				model.BATCH_CODE_B = entity.BATCH_CODE_B;
				model.OPERATE_CONTENT = entity.OPERATE_CONTENT;
				model.OPERATE_TIME = entity.OPERATE_TIME;
				model.OPERATE_USER_ID = entity.OPERATE_USER_ID;
				model.TRUENAME = entity.TRUENAME;
			
 
                return model;
            }
            else
            {
                return null;
            }
        }

		public virtual V_LOG_LMSEXCHANGEModel GetItem(Expression<Func<V_LOG_LMSEXCHANGE, bool>> whereLambda)
        {
 
                V_LOG_LMSEXCHANGE entity = m_Rep.GetItem(whereLambda);
                if (entity == null) return null;

                V_LOG_LMSEXCHANGEModel model = new V_LOG_LMSEXCHANGEModel();
                              				model.LOG_ID = entity.LOG_ID;
				model.BATCH_CODE_B = entity.BATCH_CODE_B;
				model.BATCH_CODE_A = entity.BATCH_CODE_A;
				model.OPERATE_CONTENT = entity.OPERATE_CONTENT;
				model.OPERATE_TIME = entity.OPERATE_TIME;
				model.OPERATE_USER_ID = entity.OPERATE_USER_ID;
				model.TRUENAME = entity.TRUENAME;
			
 
                return model; 
        }

        public bool isExchange(ref ValidationErrors errors, string p_code_a,string p_code_b,int p_type,string p_annex,string p_user)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    var ret = db.ExecuteProduce("PCK_LMSDATAINFO.EXCHANGE_LMS_CONRESULT",
                         db.CreateInParameter("P_PCODE_A", DbType.AnsiString, p_code_a),
                        db.CreateInParameter("P_PCODE_B", DbType.AnsiString, p_code_b),
                        db.CreateInParameter("P_TYPE", DbType.AnsiString, p_type),
                        db.CreateInParameter("P_ANNEX", DbType.AnsiString, p_annex),
                        db.CreateInParameter("P_USER", DbType.AnsiString, p_user),
                        db.CreateOutParameter("P_ERRCODE", DbType.AnsiString, 20),
                        db.CreateOutParameter("P_ERRMSG", DbType.AnsiString, 100)
                        );
                    if (ret["P_ERRCODE"].ToString() != "0")
                    {
                        errors.Add(ret["P_ERRMSG"].ToString());
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }        
            }
            catch(Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }
		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<V_LOG_LMSEXCHANGEModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<V_LOG_LMSEXCHANGEModel>(x => x.LOG_ID, "LOG_ID");
				 excelFile.AddMapping<V_LOG_LMSEXCHANGEModel>(x => x.BATCH_CODE_A, "BATCH_CODE_A");
				 excelFile.AddMapping<V_LOG_LMSEXCHANGEModel>(x => x.BATCH_CODE_B, "BATCH_CODE_B");
				 excelFile.AddMapping<V_LOG_LMSEXCHANGEModel>(x => x.OPERATE_CONTENT, "OPERATE_CONTENT");
				 excelFile.AddMapping<V_LOG_LMSEXCHANGEModel>(x => x.OPERATE_TIME, "OPERATE_TIME");
				 excelFile.AddMapping<V_LOG_LMSEXCHANGEModel>(x => x.OPERATE_USER_ID, "OPERATE_USER_ID");
				 excelFile.AddMapping<V_LOG_LMSEXCHANGEModel>(x => x.TRUENAME, "TRUENAME");
				
 
            //SheetName
            var excelContent = excelFile.Worksheet<V_LOG_LMSEXCHANGEModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new V_LOG_LMSEXCHANGEModel();
						 				  entity.LOG_ID = row.LOG_ID;
				  entity.BATCH_CODE_A = row.BATCH_CODE_A;
				  entity.BATCH_CODE_B = row.BATCH_CODE_B;
				  entity.OPERATE_CONTENT = row.OPERATE_CONTENT;
				  entity.OPERATE_TIME = row.OPERATE_TIME;
				  entity.OPERATE_USER_ID = row.OPERATE_USER_ID;
				  entity.TRUENAME = row.TRUENAME;
				 
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<V_LOG_LMSEXCHANGEModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        V_LOG_LMSEXCHANGE entity = new V_LOG_LMSEXCHANGE();
                       						entity.LOG_ID = model.LOG_ID;
						entity.BATCH_CODE_A = model.BATCH_CODE_A;
						entity.BATCH_CODE_B = model.BATCH_CODE_B;
						entity.OPERATE_CONTENT = model.OPERATE_CONTENT;
						entity.OPERATE_TIME = model.OPERATE_TIME;
						entity.OPERATE_USER_ID = model.OPERATE_USER_ID;
						entity.TRUENAME = model.TRUENAME;
						
 
                        db.V_LOG_LMSEXCHANGE.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
